package sn.hn.demo;

import java.util.ArrayList;

import sn.hn.demo.image.ImageData;
import sn.hn.demo.level.Level;
import sn.hn.demo.thread.GameLoop;
import sn.hn.demo.thread.MyThreadFish;
import sn.hn.demo.unit.Boat;
import sn.hn.demo.unit.Bubbles;
import sn.hn.demo.unit.Cloud;
import sn.hn.demo.unit.Fish;
import sn.hn.demo.unit.NumberText;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.PaintDrawable;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameSurface extends SurfaceView implements SurfaceHolder.Callback{
	private GameLoop thread;	
	private Bitmap mBK;
	public Boat mBoat;
	public boolean isCatching = false;
	public boolean isFishing;
	public boolean mFinish;
	public int mGameTime;
	public long mGameTimeLoop;
	public int mScore = 0;
	public boolean mGameOver;
	public Level mLevel;
	public ArrayList<Fish> mListFishs = new ArrayList<Fish>();
	public ArrayList<Bubbles> mListBubbles = new ArrayList<Bubbles>();
	public ArrayList<Cloud> mListCloud = new ArrayList<Cloud>();
	public final int LIMIT = 4*MainActivity.SCREENWIDTH/100;
	public Paint mPaintCloud = new Paint();
	public Paint mPaintPath = new Paint();
	public NumberText mText = new NumberText();
	public GameSurface(Context context) {
		super(context);
		if(MainActivity.hp > 1)
			mBoat = new Boat(context,MainActivity.SCREENWIDTH/2,(float)6*MainActivity.SCREENHEIGHT/24,5*MainActivity.SCREENHEIGHT/10);
		else
			mBoat = new Boat(context,MainActivity.SCREENWIDTH/2,(float)5.5*MainActivity.SCREENHEIGHT/24,5*MainActivity.SCREENHEIGHT/10);
		mGameOver = false;
		mScore = 0;
		mGameTime = 120;
		if(MainActivity.wp > 1)
			delta = 4;
		else delta = 2;
		init();
		for (int i = 0; i <= 3; i++) {
			Fish fish = new Fish(GameActivity.mGameContext);
			mListFishs.add(fish);
			Bubbles b = new Bubbles(GameActivity.mGameContext);
			mListBubbles.add(b);
			Cloud c = new Cloud(GameActivity.mGameContext);
			mListCloud.add(c);
		}
		setFocusable(true);
	}
	
	public void init()
	{
		getHolder().addCallback(this);
		thread = new GameLoop(getHolder(), this);
		mBK = ImageData.mBackground;
		this.mLevel = new Level();
		mPaintPath.setFilterBitmap(true);
		mPaintPath.setStrokeWidth(2);
		mPaintPath.setColor(Color.DKGRAY);
	}
	
	public float xStart;
	public float xEnd;
	public float d;
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			isFishing = true;
			mBoat.isMove = false;
			xStart = event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			xEnd = event.getX();
			d = Math.abs(xEnd- xStart); 
			if(d < LIMIT)
				isFishing = true;
			else{
				mBoat.isMove = true;
				if(xStart < xEnd)
					mBoat.isLeft = false;
				else mBoat.isLeft = true;
			}
			break;
		case MotionEvent.ACTION_UP:
			isFishing = false;
			break;
		default:
			break;
		}
		return true;
	}

	int delta = 0;
	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Rect dest = new Rect(0, 0, getWidth(), getHeight());
		Paint paint = new Paint();
		paint.setFilterBitmap(true);
//		paint.setColor(532919);
		canvas.drawBitmap(mBK, null, dest, paint);
		// Vẽ các thông số
		drawNoti(canvas, paint);
		// Vẽ mây
		for(int i = 0; i < mListCloud.size(); i++)
		{
			mListCloud.get(i).mSprite.draw(canvas);
		}
		// Vẽ thuyền
		if(mBoat.isLeft)
			mBoat.mSprite.init(mBoat.mBoat, mBoat.mBoat.getWidth() , mBoat.mBoat.getHeight(), 1, 0);
		else 
			mBoat.mSprite.init(mBoat.mBoatLeft, mBoat.mBoatLeft.getWidth() , mBoat.mBoatLeft.getHeight(), 1, 0);
		mBoat.mSprite.draw(canvas);
		y = mBoat.mSprite.yPos + endY;
		if(mBoat.isLeft)
		{
			this.mBoat.mSpriteMoc.xPos = mBoat.mSprite.xPos + 92*mBoat.mBoat.getWidth()/229 - mBoat.mMoc.getWidth()/2;
			this.mBoat.mSpriteMoc.yPos = this.y - 2;
		}else{
			this.mBoat.mSpriteMoc.xPos = mBoat.mSprite.xPos + 143*mBoat.mBoat.getWidth()/229 - mBoat.mMoc.getWidth()/2;
			this.mBoat.mSpriteMoc.yPos = this.y - 2;
		}
		
		mBoat.mSpriteMoc.draw(canvas);
		// Vẽ cần câu
		drawPath(canvas, new PaintDrawable(532919));
		// Vẽ cá
		for(int i = 0; i < mListFishs.size(); i++)
		{
			mListFishs.get(i).mSprite.loop(mGameTimeLoop);
			mListFishs.get(i).mSprite.draw(canvas);
		}
		// Vẽ bong bóng
		for(int i = 0; i < mListBubbles.size(); i++)
		{
			mListBubbles.get(i).mSprite.draw(canvas);
		}
	}
	
	// Vẽ dây câu
	public static int deltaPath = 0;
	public static int deltaPathLeft = 2;
	float endY = 0;
	public float y = 0;
	public void drawPath(Canvas canvas,PaintDrawable p)
	{
		if(isFishing && !this.mFinish && !isCatching){   
			if(endY <= mBoat.mLength)
			{
				endY -= MainActivity.Y_UNIT;
				if(mBoat.isLeft)
					canvas.drawLine(mBoat.mSprite.xPos + 92*mBoat.mBoat.getWidth()/229 , mBoat.mSprite.yPos, mBoat.mSprite.xPos + 92*mBoat.mBoat.getWidth()/229, y, mPaintPath);
				else
					canvas.drawLine(mBoat.mSprite.xPos + 143*mBoat.mBoat.getWidth()/229, mBoat.mSprite.yPos, mBoat.mSprite.xPos + 143*mBoat.mBoat.getWidth()/229, y, mPaintPath);
			}else{
				isFishing = false;
			}
		}else{
			if(endY != 0){
				endY += MainActivity.Y_UNIT;
				if(mBoat.isLeft)
					canvas.drawLine(mBoat.mSprite.xPos + 92*mBoat.mBoat.getWidth()/229 , mBoat.mSprite.yPos, mBoat.mSprite.xPos + 92*mBoat.mBoat.getWidth()/229, y, mPaintPath);
				else
					canvas.drawLine(mBoat.mSprite.xPos + 143*mBoat.mBoat.getWidth()/229, mBoat.mSprite.yPos, mBoat.mSprite.xPos + 143*mBoat.mBoat.getWidth()/229, y, mPaintPath);
			}
		}
	}
	
	// Vẽ text thông báo
	public void drawNoti(Canvas canvas,Paint paint)
	{
		// Vẽ màn chơi
		canvas.drawBitmap(NumberText.level, MainActivity.SCREENWIDTH/80, MainActivity.SCREENHEIGHT/32, paint);
		int level = mLevel.level + 1;
		if(level >= 10)
		{
			if(level == 10)
			{
				canvas.drawBitmap(mLevel.getBitmapLevel(1), MainActivity.SCREENWIDTH/80 + NumberText.level.getWidth() + 5, (float)1.4*MainActivity.SCREENHEIGHT/32, paint);
				canvas.drawBitmap(mLevel.getBitmapLevel(0), MainActivity.SCREENWIDTH/80 + NumberText.level.getWidth() + mLevel.getBitmapLevel(1).getWidth() + 5, (float)1.4*MainActivity.SCREENHEIGHT/32, paint);
			}else{
				int lv = level/10;
				canvas.drawBitmap(mLevel.getBitmapLevel(lv), MainActivity.SCREENWIDTH/80 + NumberText.level.getWidth() + 5, (float)1.4*MainActivity.SCREENHEIGHT/32, paint);
				canvas.drawBitmap(mLevel.getBitmapLevel(level%10), MainActivity.SCREENWIDTH/80 + NumberText.level.getWidth() + mLevel.getBitmapLevel(lv).getWidth() + 5, (float)1.4*MainActivity.SCREENHEIGHT/32, paint);
			}
		}else{
			canvas.drawBitmap(mLevel.getBitmapLevel(level%10), MainActivity.SCREENWIDTH/80 + NumberText.level.getWidth() + 5, (float)1.4*MainActivity.SCREENHEIGHT/32, paint);
		}
		// Vẽ điểm
		canvas.drawBitmap(NumberText.score, 9*MainActivity.SCREENWIDTH/20, MainActivity.SCREENHEIGHT/32, paint);
		int b = 0, t = 0, c = 0, d = 0, x = 0 ;
		b = this.mScore/1000;
		t = this.mScore/100;
		c = (this.mScore - t*100)/10;
		d = this.mScore - t*100 - c*10;
		
		if(b > 0)
			canvas.drawBitmap(NumberText.getBitmap(t), 9*MainActivity.SCREENWIDTH/20 + NumberText.score.getWidth() + 3, (float)1.55*MainActivity.SCREENHEIGHT/32, paint);
		x = 9 * MainActivity.SCREENWIDTH/20 + NumberText.score.getWidth() + 3;
		if(t > 0)
			canvas.drawBitmap(NumberText.getBitmap(t), x + NumberText.getBitmap(b).getWidth() + 2, (float)1.55*MainActivity.SCREENHEIGHT/32, paint);
		x = x + NumberText.getBitmap(b).getWidth() + 2;
		if(c >= 0)
			canvas.drawBitmap(NumberText.getBitmap(c), x + NumberText.getBitmap(t).getWidth()+ 2, (float)1.55*MainActivity.SCREENHEIGHT/32, paint);
		x = x + NumberText.getBitmap(t).getWidth()+ 2;
		canvas.drawBitmap(NumberText.getBitmap(d), x + NumberText.getBitmap(c).getWidth() + 2, (float)1.55*MainActivity.SCREENHEIGHT/32, paint);
		
		// Vẽ thời gian 
		canvas.drawBitmap(NumberText.clock, 61*MainActivity.SCREENWIDTH/80, MainActivity.SCREENHEIGHT/84, paint);
		int m = 0, s1 = 0, s2 = 0;
		m = this.mGameTime / 60;
		s1 = (this.mGameTime - m*60)/10;
		s2 = this.mGameTime - m*60 - s1*10;
		canvas.drawBitmap(NumberText.getBitmap(m), 61*MainActivity.SCREENWIDTH/80 + NumberText.clock.getWidth() +3, (float)1.55*MainActivity.SCREENHEIGHT/32, paint);
		x = 61*MainActivity.SCREENWIDTH/80 + NumberText.clock.getWidth() + 3;
		canvas.drawBitmap(NumberText.getBitmap(10), x + NumberText.getBitmap(m).getWidth() + 2,(float)1.7*MainActivity.SCREENHEIGHT/32, paint);
		x = x + NumberText.getBitmap(m).getWidth() + 2;
		canvas.drawBitmap(NumberText.getBitmap(s1), x + NumberText.getBitmap(10).getWidth() + 2,(float)1.55*MainActivity.SCREENHEIGHT/32, paint);
		x = x + NumberText.getBitmap(10).getWidth() + 2;
		canvas.drawBitmap(NumberText.getBitmap(s2), x + NumberText.getBitmap(s1).getWidth() + 2,(float)1.55*MainActivity.SCREENHEIGHT/32, paint);
		// Vẽ text khi ghi điểm
		if(MyThreadFish.updateScore)
		{
			canvas.drawText("+ " + MyThreadFish.score, 4*MainActivity.SCREENWIDTH/8, 3*MainActivity.SCREENHEIGHT/10, paint);
		}
			
			
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		if(thread.getState() == Thread.State.TERMINATED)
		{
			thread = new GameLoop(getHolder(), this);
		}
		thread.setRunning(true);
		try {
			thread.start();
		} catch (IllegalThreadStateException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		thread.setRunning(false);
		while (retry)
		{
			try
			{
				thread.join();
				retry = false;
			}
			catch (InterruptedException e){}
		}
	}
}
